<?
ob_start();
use \PFT\Db;
include("class/SimpleExcel.class.php");
include '/var/www/html/new/d/common/header.html';
include'/var/www/html/new/d/common/left.html';
include '/var/www/html/new/d/tpl/publish_coupons/publish_manage.html';
include '/var/www/html/new/d/common/footer.html';
$out_html=ob_get_contents();
ob_end_clean();

if(isset($_REQUEST['Export']) && $_REQUEST['Export']=="Export"){
    include_once '/var/www/html/new/d/module/common/Db.class.php';
    include_once '/var/www/html/new/d/common/func.inc.php';
    $dbConf = include_once '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息

    Db::Conf($dbConf['localhost']);
    $status_list = array('正常','已过期','已使用');
    $time=date('Y-m-d',$_SERVER['REQUEST_TIME']);
    $title=empty($_GET['title'])?"":"and coupon_name like '%{$_GET['title']}%'";
    $status=$_REQUEST['nor_his'];
    $sql="select id,coupon_name from pft_coupons where apply_did={$_SESSION['memberID']} and status=$status ".$title;//搜索上架的优惠劵

    $stmt =Db::Connect()->prepare($sql);
    $stmt->execute();
    $result=$id_list=$remarks=array();
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
        $result[$row['id']]=$row;
        $id_list[]=$row['id'];
    }
    $filename='优惠券列表';
    $Excel[0]['coupon_name']='赠券名称';
    $Excel[0]['coupon_id']='优惠券id';
    $Excel[0]['dname']='领用人姓名';
    $Excel[0]['mobile']='联系电话';
    $Excel[0]['create_time']='领取时间';
    $Excel[0]['start_date']='起始有效期';
    $Excel[0]['end_date']='终止有效期';
    $Excel[0]['dstatus']='状态';
    $Excel[0]['ordernum']='使用订单号';
    if(!empty($id_list)){
        $sql_coupon="select create_time,coupon_id,fid,dstatus,ordernum,coupon_id,start_date,end_date from pft_member_coupon
                 where aid={$_SESSION['memberID']} and coupon_id in(".implode(',',$id_list).")";
        $stmt =Db::Connect()->prepare($sql_coupon);
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
            $res[$row['coupon_id']][]=$row;
            $fid_list[]=$row['fid'];
        }
        if(!empty($fid_list)){
            $sql_member="select id,dname,mobile from pft_member where  id in(".implode(',',$fid_list).")";
            $stmt =Db::Connect()->prepare($sql_member);
            $stmt->execute();
            while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
                $res_member[$row['id']]=$row;
            }
            Db::Close();
            $i=1;
            foreach($res as $res_key =>$res_val){
                foreach($res_val as $key=>$val){
                    $n++;
                    $Excel[$n]['coupon_name']=$result[$res_key]['coupon_name'];
                    $Excel[$n]['coupon_id']  =$res[$res_key][$key]['coupon_id'];
                    $Excel[$n]['dname']      =$res_member[$res[$res_key][$key]['fid']]['dname'];
                    $Excel[$n]['mobile']     =$res_member[$res[$res_key][$key]['fid']]['mobile'];
                    $Excel[$n]['create_time']=$res[$res_key][$key]['create_time'];
                    $Excel[$n]['start_date'] =$res[$res_key][$key]['start_date'];
                    $Excel[$n]['end_date']   =$res[$res_key][$key]['end_date'];
                    $Excel[$n]['dstatus']    =$status_list[$res[$res_key][$key]['dstatus']];
                    $Excel[$n]['ordernum']   =$res[$res_key][$key]['ordernum'];
                }
            }
        }
    }
    $xls = new SimpleExcel('UTF-8', true,'orderList');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
    exit;
}
echo $out_html;

?>